//
//  ViewController.m
//  GeoPhoto2
//
//  Created by walkerbe on 2/16/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "ImageView.h"
#import "InfoDelegateProtocol.h"
#import "ImageInfoDataObject.h"
#import "ZipArchive.h"

@implementation ViewController

@synthesize b0;
@synthesize b1;
@synthesize b2;
@synthesize b3;
@synthesize b4;
@synthesize b5;
@synthesize b6;
@synthesize b7;
@synthesize b8;
@synthesize b0Label;
@synthesize b1Label;
@synthesize b2Label;
@synthesize b3Label;
@synthesize b4Label;
@synthesize b5Label;
@synthesize b6Label;
@synthesize b7Label;
@synthesize b8Label;
@synthesize first;
@synthesize second;
@synthesize third;
@synthesize fourth;
@synthesize fifth;
@synthesize sixth;
@synthesize seventh;
@synthesize eighth;
@synthesize ninth;


- (ImageInfoDataObject *) theInfoDataObject; 
{
    id<InfoDelegateProtocol> theDelegate = (id<InfoDelegateProtocol>) [UIApplication sharedApplication].delegate;
    ImageInfoDataObject *theDataObject;
    theDataObject = (ImageInfoDataObject*) theDelegate.theInfoDataObject;
    return theDataObject;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
  
    [super viewDidLoad];
    
    self.first = nil;
    self.second = nil;
    self.third = nil;
    self.fourth = nil;
    self.fifth= nil;
    self.sixth = nil;
    self.seventh = nil;
    self.eighth = nil;
    self.ninth = nil;
    

    
    NSMutableArray *retval = [NSMutableArray array];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *publicDocumentsDir = [paths objectAtIndex:0];   
    
    NSError *error;
    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:publicDocumentsDir error:&error];
    if (files == nil) {
        NSLog(@"Error reading contents of documents directory: %@", [error localizedDescription]);
    }
    for (NSString *file in files) {
        NSString *fullPath = [publicDocumentsDir stringByAppendingPathComponent:file];
        [retval addObject:fullPath];
    }
    NSMutableArray *imageNames = [[NSMutableArray alloc] init];
    
    //NSInteger theLen = [retval count];
    
    ZipArchive* za = [[ZipArchive alloc] init];

    for (int i=0; i<[retval count]; i++) {
        NSMutableArray *tempImageNames = [[NSMutableArray alloc] init];
        [za UnzipOpenFile:[retval objectAtIndex:i]];
        tempImageNames = [za UnzipFileTo:publicDocumentsDir overWrite:YES];
        [imageNames addObject:tempImageNames];
    }
    
    NSMutableArray *imageLocations = [[NSMutableArray alloc] init]; 
    
    for (int j=0; j<[imageNames count];j++)
         {
             NSMutableArray *another = [[NSMutableArray alloc] init];
             for (NSString *name in [imageNames objectAtIndex:j]) {
                 NSString *thePath = [publicDocumentsDir stringByAppendingPathComponent:name];
                 
                 [another addObject:thePath];
             }
             [imageLocations addObject:another];
         }
    
    NSMutableArray *temp1 = [[NSMutableArray alloc] init];
    NSMutableArray *temp2 = [[NSMutableArray alloc] init];
    NSMutableArray *temp3 = [[NSMutableArray alloc] init];
    NSMutableArray *temp4 = [[NSMutableArray alloc] init];
    NSMutableArray *temp5 = [[NSMutableArray alloc] init];
    NSMutableArray *temp6 = [[NSMutableArray alloc] init];
    NSMutableArray *temp7 = [[NSMutableArray alloc] init];
    NSMutableArray *temp8 = [[NSMutableArray alloc] init];
    NSMutableArray *temp9 = [[NSMutableArray alloc] init];
    
    if ([imageLocations count] == 1) {
        temp1 = [imageLocations objectAtIndex:0];
        self.first = temp1;
    }else if ([imageLocations count] == 2) {
        temp1 = [imageLocations objectAtIndex:0];
        self.first = temp1;
        temp2 = [imageLocations objectAtIndex:1];
        self.second = temp2;
    }else if ([imageLocations count] == 3) {
        temp1 = [imageLocations objectAtIndex:0];
        self.first = temp1;
        temp2 = [imageLocations objectAtIndex:1];
        self.second = temp2;
        temp3 = [imageLocations objectAtIndex:2];
        self.third = temp3;
    }else if ([imageLocations count] == 4) {
        temp1 = [imageLocations objectAtIndex:0];
        self.first = temp1;
        temp2 = [imageLocations objectAtIndex:1];
        self.second = temp2;
        temp3 = [imageLocations objectAtIndex:2];
        self.third = temp3;
        temp4 = [imageLocations objectAtIndex:3];
        self.fourth = temp4;
    }else if ([imageLocations count] == 5) {
        temp1 = [imageLocations objectAtIndex:0];
        self.first = temp1;
        temp2 = [imageLocations objectAtIndex:1];
        self.second = temp2;
        temp3 = [imageLocations objectAtIndex:2];
        self.third = temp3;
        temp4 = [imageLocations objectAtIndex:3];
        self.fourth = temp4;
        temp5 = [imageLocations objectAtIndex:4];
        self.fifth = temp5;
    }else if ([imageLocations count] == 6) {
        temp1 = [imageLocations objectAtIndex:0];
        self.first = temp1;
        temp2 = [imageLocations objectAtIndex:1];
        self.second = temp2;
        temp3 = [imageLocations objectAtIndex:2];
        self.third = temp3;
        temp4 = [imageLocations objectAtIndex:3];
        self.fourth = temp4;
        temp5 = [imageLocations objectAtIndex:4];
        self.fifth = temp5;
        temp6 = [imageLocations objectAtIndex:5];
        self.sixth = temp6;
    }else if ([imageLocations count] == 7) {
        temp1 = [imageLocations objectAtIndex:0];
        self.first = temp1;
        temp2 = [imageLocations objectAtIndex:1];
        self.second = temp2;
        temp3 = [imageLocations objectAtIndex:2];
        self.third = temp3;
        temp4 = [imageLocations objectAtIndex:3];
        self.fourth = temp4;
        temp5 = [imageLocations objectAtIndex:4];
        self.fifth = temp5;
        temp6 = [imageLocations objectAtIndex:5];
        self.sixth = temp6;
        temp7 = [imageLocations objectAtIndex:6];
        self.seventh = temp7;
    }else if ([imageLocations count] == 8) {
        temp1 = [imageLocations objectAtIndex:0];
        self.first = temp1;
        temp2 = [imageLocations objectAtIndex:1];
        self.second = temp2;
        temp3 = [imageLocations objectAtIndex:2];
        self.third = temp3;
        temp4 = [imageLocations objectAtIndex:3];
        self.fourth = temp4;
        temp5 = [imageLocations objectAtIndex:4];
        self.fifth = temp5;
        temp6 = [imageLocations objectAtIndex:5];
        self.sixth = temp6;
        temp7 = [imageLocations objectAtIndex:6];
        self.seventh = temp7;
        temp8 = [imageLocations objectAtIndex:7];
        self.eighth = temp8;
    }else if ([imageLocations count] == 9) {
        temp1 = [imageLocations objectAtIndex:0];
        self.first = temp1;
        temp2 = [imageLocations objectAtIndex:1];
        self.second = temp2;
        temp3 = [imageLocations objectAtIndex:2];
        self.third = temp3;
        temp4 = [imageLocations objectAtIndex:3];
        self.fourth = temp4;
        temp5 = [imageLocations objectAtIndex:4];
        self.fifth = temp5;
        temp6 = [imageLocations objectAtIndex:5];
        self.sixth = temp6;
        temp7 = [imageLocations objectAtIndex:6];
        self.seventh = temp7;
        temp8 = [imageLocations objectAtIndex:7];
        self.eighth = temp8;
        temp9 = [imageLocations objectAtIndex:8];
        self.ninth = temp9;
    }else {
        
    }
    
    //archive = files;

    //NSMutableArray *temp = [[NSMutableArray alloc] init];
    //self.selected = temp;
    //self.cellIndex = retval;
    
    
    
    
    /*
    NSMutableArray *temp1 = [[NSMutableArray alloc] initWithObjects:@"bird.jpg", @"bird2.jpg", @"bird3.jpg", @"bird4.jpg", @"bird5.jpg", nil];
    NSMutableArray *temp2 = [[NSMutableArray alloc] initWithObjects:@"rock.jpg", @"rock2.jpg", @"rock3.jpg", @"rock4.jpg", nil];
    NSMutableArray *temp3 = [[NSMutableArray alloc] initWithObjects:@"1GMO-1P.jpg", @"1GMO-1X.jpg", nil];
    
    self.first = temp1;
    self.second = temp2;
    self.third = temp3;
    */
    if (self.first){
        //UIImage *firstImage = [UIImage imageNamed:[self.first objectAtIndex:0]];
        UIImage *firstImage = [UIImage imageWithContentsOfFile:[self.first objectAtIndex:0]];
        [b0 setBackgroundImage:firstImage forState:UIControlStateNormal];
        [b0 setTitle:@"first" forState:UIControlStateDisabled];
        [b0Label setText:[[imageNames objectAtIndex:0] objectAtIndex:0]];
    }
    if (self.second){
        //UIImage *secondImage = [UIImage imageNamed:[self.second objectAtIndex:0]];
        UIImage *secondImage = [UIImage imageWithContentsOfFile:[self.second objectAtIndex:0]];
        [b1 setBackgroundImage:secondImage forState:UIControlStateNormal];
        [b1 setTitle:@"second" forState:UIControlStateDisabled];
        [b1Label setText:[[imageNames objectAtIndex:1] objectAtIndex:0]];
    }
    
    if (self.third){
        //UIImage *thirdImage = [UIImage imageNamed:[self.third objectAtIndex:0]];
        UIImage *thirdImage = [UIImage imageWithContentsOfFile:[self.third objectAtIndex:0]];
        [b2 setBackgroundImage:thirdImage forState:UIControlStateNormal];
        [b2 setTitle:@"third" forState:UIControlStateDisabled];
        [b2Label setText:[[imageNames objectAtIndex:2] objectAtIndex:0]];
    }
    if (self.fourth){
        //UIImage *thirdImage = [UIImage imageNamed:[self.fourth objectAtIndex:0]];
        UIImage *fourthImage = [UIImage imageWithContentsOfFile:[self.fourth objectAtIndex:0]];
        [b3 setBackgroundImage:fourthImage forState:UIControlStateNormal];
        [b3 setTitle:@"fourth" forState:UIControlStateDisabled];
        [b3Label setText:[[imageNames objectAtIndex:3] objectAtIndex:0]];
    }
    if (self.fifth){
        //UIImage *thirdImage = [UIImage imageNamed:[self.fifth objectAtIndex:0]];
        UIImage *fifthImage = [UIImage imageWithContentsOfFile:[self.fifth objectAtIndex:0]];
        [b4 setBackgroundImage:fifthImage forState:UIControlStateNormal];
        [b4 setTitle:@"fifth" forState:UIControlStateDisabled];
        [b4Label setText:[[imageNames objectAtIndex:4] objectAtIndex:0]];
    }
    if (self.sixth){
        //UIImage *thirdImage = [UIImage imageNamed:[self.sixth objectAtIndex:0]];
        UIImage *sixthImage = [UIImage imageWithContentsOfFile:[self.sixth objectAtIndex:0]];
        [b5 setBackgroundImage:sixthImage forState:UIControlStateNormal];
        [b5 setTitle:@"sixth" forState:UIControlStateDisabled];
        [b5Label setText:[[imageNames objectAtIndex:5] objectAtIndex:0]];
    }
    if (self.seventh){
        //UIImage *thirdImage = [UIImage imageNamed:[self.seventh objectAtIndex:0]];
        UIImage *seventhImage = [UIImage imageWithContentsOfFile:[self.seventh objectAtIndex:0]];
        [b6 setBackgroundImage:seventhImage forState:UIControlStateNormal];
        [b6 setTitle:@"seventh" forState:UIControlStateDisabled];
        [b6Label setText:[[imageNames objectAtIndex:6] objectAtIndex:0]];
    }
    if (self.eighth){
        //UIImage *thirdImage = [UIImage imageNamed:[self.eighth objectAtIndex:0]];
        UIImage *eighthImage = [UIImage imageWithContentsOfFile:[self.eighth objectAtIndex:0]];
        [b7 setBackgroundImage:eighthImage forState:UIControlStateNormal];
        [b7 setTitle:@"eighth" forState:UIControlStateDisabled];
        [b7Label setText:[[imageNames objectAtIndex:7] objectAtIndex:0]];
    }
    if (self.ninth){
        //UIImage *thirdImage = [UIImage imageNamed:[self.ninth objectAtIndex:0]];
        UIImage *ninthImage = [UIImage imageWithContentsOfFile:[self.ninth objectAtIndex:0]];
        [b8 setBackgroundImage:ninthImage forState:UIControlStateNormal];
        [b8 setTitle:@"ninth" forState:UIControlStateDisabled];
        [b8Label setText:[[imageNames objectAtIndex:8] objectAtIndex:0]];
    }
    
    /*
    UIImage *bird = [UIImage imageNamed:@"bird5.jpg"];
    [b0 setBackgroundImage:bird forState:UIControlStateNormal];
    [b0 setTitle:@"rock.jpg" forState:UIControlStateDisabled];
    [b0Label setText:@"rock.jpg"];
     */
        
}



-(IBAction)buttonPressed:(id)sender{
    NSString *name = [[NSString alloc] init];
    name = [sender titleForState:UIControlStateDisabled];
    ImageInfoDataObject *theDataObject = [self theInfoDataObject];

    if ([name isEqualToString:@"first"]) {
        theDataObject.theInfo = self.first;
        [self performSegueWithIdentifier:@"toImageView" sender:sender];
    }
    else if ([name isEqualToString:@"second"]) {
        theDataObject.theInfo = self.second;
        [self performSegueWithIdentifier:@"toImageView" sender:sender];
    }
    else if ([name isEqualToString:@"third"]) {
        theDataObject.theInfo = self.third;
        [self performSegueWithIdentifier:@"toImageView" sender:sender];
    }
    else if ([name isEqualToString:@"fourth"]) {
        theDataObject.theInfo = self.fourth;
        [self performSegueWithIdentifier:@"toImageView" sender:sender];
    }
    else if ([name isEqualToString:@"fifth"]) {
        theDataObject.theInfo = self.fifth;
        [self performSegueWithIdentifier:@"toImageView" sender:sender];
    }
    else if ([name isEqualToString:@"sixth"]) {
        theDataObject.theInfo = self.sixth;
        [self performSegueWithIdentifier:@"toImageView" sender:sender];
    }
    else if ([name isEqualToString:@"seventh"]) {
        theDataObject.theInfo = self.seventh;
        [self performSegueWithIdentifier:@"toImageView" sender:sender];
    }
    else if ([name isEqualToString:@"eighth"]) {
        theDataObject.theInfo = self.eighth;
        [self performSegueWithIdentifier:@"toImageView" sender:sender];
    }
    else if ([name isEqualToString:@"nine"]) {
        theDataObject.theInfo = self.ninth;
        [self performSegueWithIdentifier:@"toImageView" sender:sender];
    }
    

}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    
    
    
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return UIInterfaceOrientationPortrait;
}

@end
